package com.ltf.controller.stu;

import com.ltf.entity.*;
import com.ltf.entity.*;
import com.ltf.entity.*;
import com.ltf.service.MarkService;
import com.ltf.service.TaskService;
import com.ltf.service.XUserService;
import com.ltf.utils.PackJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/fore/stu")
public class MyStuController {
    @Autowired
    XUserService userService;
    @Autowired
    TaskService taskService;
    @Autowired
    MarkService markService;

    public MyStuController(){}

    @GetMapping("/findMyCourse")
    public XJson findMyCourse(){
        Object phone = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        XUser newUser = this.userService.findByPhone((String) phone);
        if (newUser == null) {
            return PackJson.packX(404, "用户未发现", (Object)null);
        } else {
            List<Task> tasks = this.taskService.findByClzno(newUser.getClz().getClzno());
            System.out.println(tasks);
            return PackJson.packX(200, "", tasks);
        }
    }

    @GetMapping("/findMyMark")
    public XJson findMyMark(){
        Object phone = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        XUser newUser = this.userService.findByPhone((String) phone);
        if (newUser == null) {
            return PackJson.packX(404, "用户未发现", (Object)null);
        } else {
            List<Mark> marks = this.markService.findBySno(newUser.getPhone());
            System.out.println(marks);
            return PackJson.packX(200, "", marks);
        }
    }
    @PostMapping("/findByClznoAndCno")//查询某班级某课程的评分
    public XJson findMark(String clzno,String role,Integer termId){
        System.out.println("clzno="+clzno);
        System.out.println("role="+role);
        System.out.println("termId="+termId);
        List<MarkInfo> byClznoCno = markService.findByClzCno(clzno, role,termId);
        return PackJson.packX(200,"",byClznoCno);
    }
    @PostMapping("/addMark")//添加评分
    public XJson addMark(@RequestBody List<Mark> marks){
        markService.doMark(marks);
        return PackJson.packX(200, "", "");
    }

//    // 获取教师评分数据
//    @GetMapping("/findTeacherScores")
//    public XJson findTeacherScores() {
//        Object phone = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//        XUser newUser = this.userService.findByPhone((String) phone);
//        if (newUser == null) {
//            return PackJson.packX(404, "用户未发现", (Object)null);
//        } else {
//            // 这里需要根据实际情况实现获取教师评分数据的逻辑
//            // 假设通过 MarkService 来获取相关数据
//            List<Mark> scores = markService.findTeacherScores(newUser.getPhone());
//            return PackJson.packX(200, "", scores);
//        }
//    }
//
//    // 提交评分
//    @PostMapping("/submitScores")
//    public XJson submitScores(@RequestBody List<Mark> scores) {
//        try {
//            // 这里需要根据实际情况实现提交评分的逻辑
//            System.out.println(scores);
//            // 假设通过 MarkService 来处理提交评分
//            markService.doMark(scores);
////            markService.submitScores(scores);
//            return PackJson.packX(200, "评分提交成功", null);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return PackJson.packX(500, "评分提交失败", null);
//        }
//    }
}